<template>
<div>
  <p>Text Encrypter</p>
  <form @submit.prevent="encryptText(text, rsaPubKey);">
    <input type="text" v-model="text" />
    <button>Submit</button>
  </form>
  <pre>{{rsaPubKey !== undefined ? "has key" : "no key" }}</pre>
  <pre>{{encryptedText}}</pre>
</div>
</template>

<script>
export default {
    name: "TextEncrypter",
    props: ["rsaPubKey"],
    data() {
        return {
            text: '',
            encryptedText: ''
        }
    },
    methods: {
        encryptText: function(toEncrypt, pubkey) {
            const crypto = require("crypto");
            const constants = require("constants");

            const bufferToEncrypt = new Buffer(toEncrypt);
            const padding = constants.RSA_PKCS1_PADDING;
            const encrypted = crypto.publicEncrypt(
                {
                    key: pubkey,
                    padding: padding,
                },
                bufferToEncrypt);

            this.encryptedText = encrypted.toString("base64");

        },
    },
}
</script>
